//
//  AppDelegate.m
//  LGSanofiPatient
//
//  Created by 1234 on 2022/8/24.
//  123

#import "AppDelegate.h"
#import "AppDelegate+Notification.h"

#import "LGTabBarViewController.h"
#import "LGNavigationController.h"
#import "LGLoginViewController.h"
#import "ViewController.h"
#import <ImSDK_Plus/ImSDK_Plus.h>
#import "TUILogin.h"
#import <Bugly/Bugly.h>

//测试
#import "LGCancerTreatRecordController.h"
#import "LGCancerPatientInspectController.h"
#import "LGCancerPatientSymptomController.h"
#import "LGPatientCarePlanController.h"
#import "LGHomeMonitoringController.h"
#import "LGHeartRhythmController.h"
#import "LGCancerMonitoringController.h"
#import "LGPatientBasicInfoController.h"
#import "LGNormalClinicController.h"
#import "LGPatientRiskMonitorController.h"
#import "LGCareTemplateImportController.h"
#import "LGECGDetailViewController2.h"
#import "LGLungFollowPatientController.h"
#import "LGLungFollowEditController.h"
#import "LGLungFollowSendController.h"
#import "LGUploadFilesController.h"
#import "LGDeviceSendViewController.h"
#import "LGBreathRecordController.h"
#import "LGPatientDetailController2.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self registNotification:application andOptions:launchOptions];
    [Bugly startWithAppId:BuglyID];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    if (@available(iOS 13.0,*)) {
        self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //统一设置
    if (@available(iOS 11.0,*)) {
        [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [UITableView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 15.0,*)) {
        [UITableView appearance].sectionHeaderTopPadding = 0;
    }
    
    if (kIsLogin && [kIsLogin isEqualToString:@"Y"]) {
        //已登录
        [LGLoginManager loginTencentCloud];//登录腾讯云
        LGTabBarViewController *controller = [[LGTabBarViewController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController = controller;
        
        //测试
//        [self checkTestController];

    }else{
        LGNavigationController *navController = [[LGNavigationController alloc]initWithRootViewController:[[LGLoginViewController alloc] init]];
        self.window.rootViewController = navController;
    }
    //显示启动页的时间
    [NSThread sleepForTimeInterval:1];
    
    return YES;
}

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    [self openFileWithOpenUrl:url];
    return YES;
}

-(void)openFileWithOpenUrl:(NSURL *)url{
    if (kIsLogin && [kIsLogin isEqualToString:@"Y"]) {
        [LGLoginManager loginTencentCloud];//登录腾讯云
        if ([UIApplication sharedApplication].keyWindow.rootViewController) {
            LGTabBarViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;
            [controller goToUploadFile:url];
        }else{
            LGTabBarViewController *controller = [[LGTabBarViewController alloc]init];
            [UIApplication sharedApplication].keyWindow.rootViewController = controller;
            [controller goToUploadFile:url];
        }
    }
}

//测试跳转
-(void)checkTestController{
    //ViewController
//    ViewController *controller = [[ViewController alloc]init];
    //市一 -- 慢阻肺、哮喘的患者档案
    LGPatientDetailController2 *controller = [[LGPatientDetailController2 alloc]init];
    controller.patientid = @"540";
    controller.diseaseCode = @"6";
    controller.levelid = @"22";
    
    //癌症 -- 患者详情 -- 治疗记录
//    LGCancerTreatRecordController *controller = [[LGCancerTreatRecordController alloc]init];
//    controller.patientid = @"2";
    
    //癌症 -- 患者详情 -- 检验检查
//    LGCancerPatientInspectController *controller = [[LGCancerPatientInspectController alloc]init];
//    controller.patientid = @"3";
    
    //癌症 -- 患者详情 -- 症状
//    LGCancerPatientSymptomController *controller = [[LGCancerPatientSymptomController alloc]init];
//    controller.patientid = @"3";
    
    //癌症 -- 批量添加随访计划 -- 选择患者
//    LGLungFollowPatientController *controller = [[LGLungFollowPatientController alloc]init];
    //癌症 -- 批量添加随访计划 -- 新建随访计划
//    LGLungFollowSendController *controller = [[LGLungFollowSendController alloc]init];
//    controller.planid = @"42";
   
    
    //房颤 -- 照护计划
//    LGPatientCarePlanController *controller = [[LGPatientCarePlanController alloc]init];
//    controller.patientid = @"18";
    
    //房颤 -- 居家监测
//    LGHomeMonitoringController *controller = [[LGHomeMonitoringController alloc]init];
//    controller.currentIndex = 1;
//    controller.patientid = @"3";
    
    //房颤 -- 心律监测
//    LGHeartRhythmController *controller = [[LGHeartRhythmController alloc]init];
//    controller.patientid = @"3";
    
    //癌症 -- 居家监测
//    LGCancerMonitoringController *controller = [[LGCancerMonitoringController alloc]init];
//    controller.currentIndex = 1;
//    controller.patientid = @"23"; //23   24
//    controller.diseaseCode = @"2"; //2   3
    
    //房颤 -- 常规门诊-慢病复查
//    LGNormalClinicController *controller = [[LGNormalClinicController alloc]init];
//    controller.patientid = @"32"; //23   24
//    controller.diseaseCode = @"1";
//    controller.visit_id = @"179";
//    controller.visit_date = @"2024-08-22";
    
    //房颤 -- 风险监测
//    LGPatientRiskMonitorController *controller = [[LGPatientRiskMonitorController alloc]init];
//    controller.patientid = @"32";
    
    //房颤 -- 科帕奇心电图
//    LGECGDetailViewController2 *controller = [[LGECGDetailViewController2 alloc]init];
//    controller.ecgId = @"170";
//    controller.patientid = @"23";
    
    //上传报告文件
//    LGUploadFilesController *controller = [[LGUploadFilesController alloc]init];
    
    //设备发货
//    LGDeviceSendViewController *controller = [[LGDeviceSendViewController alloc]init];
    
    //呼吸训练统计
//    LGBreathRecordController *controller = [[LGBreathRecordController alloc]init];
//    controller.patientid = @"548";
    
    //=====
    LGNavigationController *navController = [[LGNavigationController alloc]initWithRootViewController:controller];
    self.window.rootViewController = navController;
}

-(void)checkAnonymous{
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    // 时间格式,此处遇到过坑,建议时间HH大写,手机24小时进制和12小时禁止都可以完美格式化
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *start = [dateFormat dateFromString:@"2024-08-13 09:30"];
    NSDate *expire = [dateFormat dateFromString:@"2024-08-14 10:00"];
    
    if ([today compare:start] == NSOrderedDescending && [today compare:expire] == NSOrderedAscending) {
        [[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"isFree"];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"isFree"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark
#pragma mark ====> APP生命周期
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
//    [self.fwDelegate applicationWillResignActive:application];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
//    @"HomeAfterDoctorViewController"
    
//    UIViewController *vc = [self getCurrentVC];
//    NSLog(@"%@",vc);
//    [self.fwDelegate applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    
//    UIViewController *vc = [self getCurrentVC];
//    NSLog(@"%@",vc);
//
//    [self.fwDelegate applicationWillEnterForeground:application];
    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        UIViewController *vc = [self getCurrentVC];
//        NSLog(@"%@",[NSThread currentThread]);
//        if ([vc isKindOfClass:[PatientDetailViewController class]] && ((PatientDetailViewController *)vc).patientType == conversationPatientType) {
//            [[RCIMClient sharedRCIMClient] clearMessagesUnreadStatus:ConversationType_PRIVATE targetId:((PatientDetailViewController *)vc).tokenid];
//            [UIApplication sharedApplication].applicationIconBadgeNumber = [[RCIMClient sharedRCIMClient] getUnreadCount:@[@(ConversationType_PRIVATE)]];
//        }
//    });
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//    [self.fwDelegate applicationDidBecomeActive:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
//    [self.fwDelegate applicationWillTerminate:application];
}


@end
